package de.schlinke_marius.adapters;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import de.schlinke_marius.R;

public class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;

	private String[] mImageIds;

	public ImageAdapter(Context c, int count, String [] ids) {
		mContext = c;
		TypedArray attr = mContext
				.obtainStyledAttributes(R.styleable.Gallery);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.Gallery_android_galleryItemBackground, 0);
		attr.recycle();
		mImageIds = new String[count];
		mImageIds = ids;
	}

	public int getCount() {
		return mImageIds.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(mContext);
		String uri = "drawable/" + mImageIds[position];
		
		imageView.setImageResource(mContext.getResources().getIdentifier(uri, null, mContext.getPackageName()));
		imageView.setLayoutParams(new Gallery.LayoutParams(340, 256));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(mGalleryItemBackground);

		return imageView;
	}
}

/*
 *   String uri = "drawable/icon";

// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
 */